public class Test2 {
    public static void main4(String[] args) {
        try {
            System.out.println(10 / 0);
            int[] array1 = new int[]{1,2,3};
            System.out.println(array1[6]);
            int[] array = null;
            System.out.println(array.length);
        } catch (Exception e) {
            System.out.println("异常");
        }
    }
    public static void main2(String[] args) {
        try {
            System.out.println(10 / 0);
        } catch (ArithmeticException e) { //catch需捕获到对应的异常，否则程序依旧异常终止
            e.printStackTrace(); //打印异常信息
            System.out.println("捕获异常");
            return;
        } finally {
            //finally中的代码一定会被执行，哪怕之前的代码中存在return
            System.out.println("finally一定被执行");
            //不建议在finally中使用return语句
        }
        System.out.println("666"); //可以执行到
    }

    public static void main5(String[] args) {
        try {
            System.out.println(10 / 0);
            int[] array1 = new int[]{1, 2, 3};
            //System.out.println(array1[6]);
            int[] array = null;
            System.out.println(array.length);
        } catch (ArithmeticException e) {
            System.out.println("捕获算术异常");
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("数组越界异常");
        } catch (NullPointerException e) {
            System.out.println("空指针异常");
        }
    }
        public static void main3(String[] args) {
        //使用try-catch捕获异常
        try {
            System.out.println(10 / 0);
        } catch (ArithmeticException e) { //catch需捕获到对应的异常，否则程序依旧异常终止
            e.printStackTrace(); //打印异常信息
            System.out.println("捕获异常");
        }
        System.out.println("666"); //可以执行到



//        System.out.println(10 / 0); //程序异常终止
//        System.out.println("666"); //执行不到这行代码
    }


    public static void test() {
        int a = 10;
        if (a == 10) {
            //throw 常用于抛出自定义异常
            throw new ArithmeticException("a == 10"); //运行时异常
        }
    }

    //通过throws抛出异常，交给调用者处理（JVM）
    public static void main1(String[] args) throws CloneNotSupportedException {
        int a = 10;
        if (a == 10) {
            //throw 常用于抛出自定义异常
            throw new CloneNotSupportedException("a == 10"); //编译时异常
        }
    }

}
